package cn.schoolwow.util.module.query.reflection.service;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.module.query.reflection.domain.ReflectionOption;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class GetClassFieldArrayFlow implements BusinessFlow {

    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ReflectionOption reflectionOption = flowContext.checkInstanceData(ReflectionOption.class);

        Class clazz = reflectionOption.clazz;
        List<Field> fieldList = new ArrayList<>();
        while(null!=clazz){
            Field[] fields = clazz.getDeclaredFields();
            Field.setAccessible(fields, true);
            for(Field field:fields){
                if(reflectionOption.ignoreFinalField&&Modifier.isFinal(field.getModifiers())){
                    continue;
                }
                if(reflectionOption.ignoreStaticField&&Modifier.isStatic(field.getModifiers())){
                    continue;
                }
                if(!reflectionOption.filterFieldNames.isEmpty()&&!reflectionOption.filterFieldNames.contains(field.getName())){
                    continue;
                }
                if(null!=reflectionOption.fieldFilter&&!reflectionOption.fieldFilter.filterField(field)){
                    continue;
                }
                fieldList.add(field);
            }
            clazz = clazz.getSuperclass();
        }
        flowContext.putTemporaryData("fieldList", fieldList);
    }

    public String name() {
        return "获取反射类所有字段";
    }
}
